home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / tcisam.zip / IOPEN.C < prev    next >
Text File  |  1987-08-21  |  4KB  |  129 lines

  1. /*
  2.  * IOPEN.C - open and close functions
  3.  *
  4.  *                      Copyright (c) 1987, Jim Mischel
  5.  * Modifications:
  6.  *
  7.  * 08/13/87 - jim - original coding
  8.  */
  9.  
  10. #include "inxdefs.h"
  11. /*
  12.  * iopen() - open file for indexed access and setup fields in control record.
  13.  * If successful, returns a pointer to the control record.  Returns NULL
  14.  * if unsuccessful.  If the routine is unable to open an existing file,
  15.  * a new file is created.  It is possible to create a new index for an
  16.  * existing data file, or a new data file for an existing index file.
  17.  * In either case, no data will be lost, but new data could be added to
  18.  * the end of the file.
  19.  */
  20. void *iopen(char *fname, unsigned recsiz, char keytyp, unsigned offset,
  21.             char dupflag, int (*cmp_rtn)())
  22. {
  23.   register df_rec *db_control;
  24.   char *path;
  25.   inx_rec *irec;
  26.  
  27.   /* allocate space for control record */
  28.   if ((db_control = malloc(sizeof(df_rec))) == NULL) {
  29.     ierror(I_NOMEM);                    /* out of memory */
  30.     return(NULL);
  31.   }
  32.  
  33.   /* allocate space for data buffer */
  34.   if ((db_control->df_dat_buff = malloc(recsiz)) == NULL) {
  35.     ierror(I_NOMEM);                    /* out of memory */
  36.     goto free_control;
  37.   }
  38.  
  39.   /* allocate space for pathname */
  40.   if ((db_control->df_path = malloc(strlen(fname))) == NULL) {
  41.     ierror(I_NOMEM);
  42.     goto free_datbuf;
  43.   }
  44.  
  45.   /* copy pathname to control record */
  46.   strcpy(db_control->df_path,fname);
  47.  
  48.   /* open index file */
  49.   if ((path = malloc(256)) == NULL) {
  50.     ierror(I_NOMEM);                    /* out of memory */
  51.     goto free_fname;
  52.   }
  53.  
  54.   strcpy(path,fname);
  55.   if ((db_control->df_inx_file = fopen(strcat(path,".INX"),"r+b")) == NULL) {
  56.     /* can't open index file, so create a new one */
  57.     if ((db_control->df_inx_file = fopen(path,"w+b")) == NULL) {
  58.       ierror(I_NOINX);                  /* couldn't create index file */
  59.       goto free_path;
  60.     }
  61.     else {
  62.       /* new index file created.  Add header node */
  63.       irec = &db_control->df_inx_buff;
  64.       irec->if_dat_ptr = 0L;
  65.       irec->if_right_node = 0L;
  66.       irec->if_left_node = 0L;
  67.       irec->if_parent = 0L;
  68.       irec->if_flags = BTHRD+ETHRD+LTHRD+RTHRD;
  69.       if (iwrite_inx(db_control,irec,0L)) {
  70.         ierror(I_INXWT);                /* couldn't write header record */
  71.         goto free_path;
  72.       }
  73.     }
  74.   }
  75.  
  76.   strcpy(path,fname);
  77.   if ((db_control->df_dat_file = fopen(strcat(path,".DAT"),"r+b")) == NULL) {
  78.     /* can't open data file, so create a new one */
  79.     if ((db_control->df_dat_file = fopen(path,"w+b")) == NULL) {
  80.       ierror(I_NODAT);                  /* couldn't create data file */
  81.       goto close_inx;
  82.     }
  83.   }
  84.  
  85.   /* setup key comparison routine. */
  86.   if (cmp_rtn != NULL)
  87.     db_control->df_cmp = cmp_rtn;
  88.   else
  89.     db_control->df_cmp = icmp_rtns[keytyp];
  90.  
  91.   db_control->df_rec_size = recsiz;
  92.   db_control->df_key_offset = offset;
  93.   db_control->df_key_ptr = db_control->df_dat_buff + offset;
  94.   db_control->df_nxt_ptr = 0L;
  95.   db_control->df_inx_ptr = 0L;
  96.   db_control->df_dat_ptr = 0L;
  97.   db_control->df_flags = DF_EOF + DF_TOF + DF_DELETE + ((dupflag) ? DF_DUP : 0);
  98.  
  99.   free(path);                           /* don't need this anymore */
  100.   ierror(0);                            /* no problem */
  101.   return(db_control);
  102.  
  103. close_inx:
  104.   fclose(db_control->df_inx_file);
  105. free_path:
  106.   free(path);
  107. free_fname:
  108.   free(db_control->df_path);
  109. free_datbuf:
  110.   free(db_control->df_dat_buff);
  111. free_control:
  112.   free(db_control);
  113.   return(NULL);
  114. } /* iopen */
  115.  
  116. /*
  117.  * iclose() - close files and free space used by control record
  118.  */
  119. void iclose(void *d)
  120. {
  121.   register df_rec *db_control = (df_rec *)d;
  122.  
  123.   fclose(db_control->df_dat_file);
  124.   fclose(db_control->df_inx_file);
  125.   free(db_control->df_path);
  126.   free(db_control->df_dat_buff);
  127.   free(db_control);
  128. } /* iclose */
  129.